package com.woniuxy.controllers;


import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.TbCustomer;
import com.woniuxy.entitys.TbOrder;
import com.woniuxy.services.OrderService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.LoginCustomer;
import com.woniuxy.utils.LoginUser;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @author wu
 * @date 2024/01/09 14:47:45
 */
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JWTUtils jwtUtils;
    //客户下单进行订单添加,改变床的状态
    @PostMapping("/order/insert")
    public ResultVO insertOrder(@RequestBody TbOrder tbOrder) {
        System.out.println(tbOrder.getBid());
        int count = orderService.insertOrder(tbOrder);
        return count > 0 ? ResultVO.success("订单增加成功") : ResultVO.failure("订单增加失败");
    }

    @PostMapping("/customer/orderListByCid")
    public ResultVO getOrderListByCid(@RequestBody Map<String, Object> map) {
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        String cidStr = (String) map.get("cid");
        Integer cid = Integer.parseInt(cidStr);
        PageInfo<TbOrder> pageInfo = orderService.getOrdersByCid(pageNum, pageSize, cid);
        return pageInfo != null ? ResultVO.success("success", pageInfo) : ResultVO.failure("failed");

    }
    @PostMapping("/order/getOrderByCid")
    public ResultVO getOrderByCid(@RequestBody Map<String, Object> map){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String header = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(header);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginCustomer loginCustomerr = (LoginCustomer) valueOperations.get("customer:" + username);
        Integer cid = loginCustomerr.getCustomer().getCid();
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<TbOrder> order = orderService.selOrderByCid(pageNum, pageSize,cid);
        return order!=null?ResultVO.success("success",order):ResultVO.failure();
    }
    @GetMapping("order/back/{id}")
    public ResultVO guihuan(@PathVariable Integer id){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String header = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(header);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginCustomer loginCustomerr = (LoginCustomer) valueOperations.get("customer:" + username);
        Integer cid = loginCustomerr.getCustomer().getCid();
        Integer i = orderService.guihuan(id,cid);
        return i > 0 ? ResultVO.success("") : ResultVO.failure("");
    }
}